$(function(){
    //用户名正则，4到16位（字母，数字，下划线，减号）
    var uPattern = /^[a-zA-Z0-9]{6,16}$/;
    
    var logopath = "";

    $("input[name=imgs]").change(function(){
        //将表单数据提交给后台
        //1、获取表单的dom对象
        var formdom = $("#form-logo")[0];
        //2、创建表单数据对象
        var formdata = new FormData(formdom);
        //3、发送ajax请求
        $.ajax({
            url: "/api/upload-img",
            method: "POST",
            data: formdata,
            contentType: false,//数据编码格式不用jquery方式
            processData: false,//传输的数据，不被jquery封装
            success: function(data){
                if(data.status == 0){
                    $("#img100")[0].src = "/"+data.data.imgpath[0];
                    logopath = "/"+data.data.imgpath[0];
                }else if(data.status == 1){
                    myalert(data.message)
                }
            },
            error: function(err){
                console.log(err)
            }
        })
    });

    $("#register").click(function(){
        if(!$("#username").val()){
            $("#username").attr({
                placeholder:"用户名不能为空"
            })
        }else if(!$("#password").val()){
            $("#password").attr({
                placeholder:"密码不能为空"
            })
        }else if(!uPattern.test($("#password").val())){
            myalert("密码必须是6-16位字母或数字")
        }else if(!$("#repassword").val()){
            $("#repassword").attr({
                placeholder:"请再次输入密码"
            })
        }else if($("#password").val()!=$("#repassword").val()){
            document.getElementById("repassword").value = "";
            $("#repassword").attr({
                placeholder:"密码不一致"
            })
        }else if(!logopath){
            myalert("头像不能为空")
        }else if($("#username").val()&&$("#password").val()&&$("#repassword").val()&&($("#password").val()==$("#repassword").val())){
            $.ajax({
                url:"/api/register",
                method:"POST",
                data:{
                    username:$("#username").val(),
                    password:$("#password").val(),
                    logo:logopath
                },
                success: function(data){
                    if(data.status == 0){
                        myalert(data.message);
                        location.href = "/login";
                    }else{
                        myalert(data.message)
                    }
                },
                error: function(error){
                    console.log(error);
                }
            })
        }
    });
    $("#login").click(function(){
        if(!$("#username").val()){
            $("#username").attr({
                placeholder:"用户名不能为空"
            })
        }else if(!$("#password").val()){
            $("#password").attr({
                placeholder:"密码不能为空"
            })
        }else if($("#username").val()&&$("#password").val()){
            $.ajax({
                url:"/api/login",
                method:"POST",
                data:{
                    username:$("#username").val(),
                    password:$("#password").val()
                },
                success: function(data){
                    if(data.status == 0){
                        myalert(data.message);
                        location.href = "/goodslist";
                    }else{
                        myalert(data.message)
                    }
                },
                error: function(error){
                    console.log(error);
                }
            })
        }
    }) 
})

function myalert(str){
    if(document.getElementsByClassName("my-alert").length == 0){
        new Promise(function(resolve,reject){
            var odiv = document.createElement("div");
            odiv.className = "my-alert";
            odiv.innerHTML = str;
            document.body.appendChild(odiv);
            resolve(odiv)
        }).then(function(odiv){
            var outtimer = setTimeout(function(){
                odiv.remove();
                clearTimeout(outtimer);
            },1000);
        })
    }
}